          SUBROUTINE (OID,GEN,LDID,IQPRC,PRC.ERROR,IQCOGS,COMM.MODE,NO.LD,OLD.IQPRC,LOG.CMT,GP.ERR,GP.DIRECT,DO.ACTS,ACTIONS,RESPS,STOP.FLOW,GP.INFO.ID)
** Version# 44.0001[14] - 04/10/2014 - 04:31pm - TSMITH - eclipse
*** V44.0001 Change - Custom Coding . - 04/10/2014 - TSMITH - eclipse

*** Subroutine : OE.GP.CHECK
*-------------------------------------------------------------------------*
*** This routine will check the current GP% of an item on an order (IQPRC
*** and IQCOGS) and see if the GP% is less than the minimum GP% allowed
*** for the user in User Maintenance. If it is below the minimum,
*** authorization is required to override. If authorization is not given,
*** PRC.ERROR is set to yes and the price change is not allowed.
*-------------------------------------------------------------------------*
*** Parameters: (Note: [ ] - indicates optional parameter)
*** OID          - Order ID                                           (IN)
*** GEN          - Generation                                         (IN)
*** LDID         - ID of Line Item to check                           (IN)
*** [IQPRC]      - Price of Item (used to calculate GP%)              (IN)
*** PRC.ERROR    - Price Error (Val: 1 on error,0 otherwise)          (OUT)
*** [IQCOGS]     - COGS/Cost of Item (used to calculate GP%)          (IN)
*** [COMM.MODE]  - Boolean varible to indicate if GP% should calc     (IN)
***                based on COGS or Comm Cost
***                (Yes->Comm Cost, No/Null->COGS)
*** [NO.LD]      - Boolean varible to indicate if the GP% should be   (IN)
***                calc'd based on Ledger Detail or based on IQPRC
***                and IQCOGS (passed in)
***
*** NOTE: Following parameters only used when NO.LD it true
***
*** [OLD.IQPRC]  - Value of Price prior to GP% change                 (IN)
*** [LOG.CMT]    - Assumed that Ledger Detail is not value, so we  (IN/OUT)
***                need to know what to put in the log (no change
***                logged if left NULL):
***                "Pricing Below the User Defined Minimum GP%"
***                will be placed preceding LOG.CMT automatically
*** GP.DIRECT    - if this param is not null, then NO.LD has to be false
***              - it is the gp% entered directly in GP% column. If this is
***                not null, then don't go calculate GP% based on Sales and
***                cogs, use it directly
***
*** The following parameter will be used for Solar only
***
*** GP.INFO.ID  - The ID used to write the gp information to the     (OUT)
***                file
*-------------------------------------------------------------------------*
*-------------------------------------------------------------------------*
*** Files required to be valid when calling routine:
*** LD   - Ledger Detail  (*NOTE: Can be NULL if NO.LD is true)
*** LED  - Ledger
*** PRD  - Product        (*NOTE: Can be NULL if NO.LD is true)
*** PLNE - Price Line     (*NOTE: Can be NULL if NO.LD is true)
*-------------------------------------------------------------------------*
*** Global Parameters Changed: -None-
*-------------------------------------------------------------------------*
*** Global Parameters required:
*** JAVA.PROC$ - Current Process is a Java Process
*-------------------------------------------------------------------------*
*** Phantom Friendly - YES
*** Java Friendly - YES
*-------------------------------------------------------------------------*
*** Initialize variables BEFORE returning -- undefined variable errors may
*** occur otherwise
          STOP.FLOW  = NO
          PRC.ERROR  = NO
          GOT.AUTH   = NO
          GP.CHANGED = NO
          OVERRIDEN  = NO

          * Solar Only
          GP.INFO.ID = ""

          *** If we have an EDI order being created on a phantom
          *** then we don't want we don't want to do the GP check
          *** because if we fail the check, then the line items
          *** will get dropped from the order.
          IF LED(30) = 'EDI' AND PHANTOM.PROC AND NOT(JAVA.PROC$) THEN
             RETURN
          END


          *** If we're adding the errors to the actions array, then java
          *** shouldn't require special logic.
          JAVA.SPEC  = (JAVA.PROC$ AND NOT(DO.ACTS))

*** Make sure LD is current for java procs.
          IF JAVA.SPEC AND NOT(LD(1)) AND NOT(NO.LD) THEN
             LD.GET LDID
          END

          *** GP.CHECKED is only used for java
          GP.CHECKED = NO
          IF GP.ERR#'' THEN GP.CHECKED = YES

          TEMP.ERR = ''
          *- First thing check to see if this is a return order
          *- if so we dont really need to check GP, so just return.
          OE.GET.QSIGN QSIGN,OID,GEN

          IF NOT(NO.LD) THEN
             IF LD(4) * QSIGN < 0 THEN RETURN
          END

          IF LED(6)<1,GEN> = 'Y' THEN RETURN

*** Depending on whether we're dealing with a Stock or Direct Order, we
*** need to set up Value Mark and Sub Value Mark positions accordingly.
          IF LED(6)<1,GEN> = 'D' THEN
             *** For Product & Pricing level min. GP%
             MIN.GP.SVM  = 2
             *** For User level min. GP%
             MIN.GP.VM   = 3
             *** For User level max. Discount %
             MAX.DISC.VM = 4
          END ELSE
             *** For Product & Pricing level min. GP%
             MIN.GP.SVM  = 1
             *** For User level min. GP%
             MIN.GP.VM   = 1
             *** For User level max. Discount %
             MAX.DISC.VM = 2
          END

*** If this is a java process, setup the gp information for this order to
*** pass back and forth to the client.
          IF JAVA.SPEC THEN
             READ GP.INFO FROM CTRLFILE,"GP.ERRORS~":USER.ID:"~":OID ELSE
                GP.INFO = ''
             END
          END


*** Setup values if NO.LD is true (do not use Ledger Detail)
          IF NO.LD THEN
             IF IQPRC      = '' THEN IQPRC = 0
             IF IQCOGS     = '' THEN IQCOGS = 0
             IF OLD.IQPRC  = '' THEN OLD.IQPRC = 0
          END

          GOSUB GET.GP

          MAX.DS = LED(35)<1,MAX.DISC.VM>
          IF MAX.DS = '' THEN
             MAX.DS = SECURITY<24,MAX.DISC.VM>
          END

          IF MIN.GP#'' THEN GOSUB CHK.GP

*** This routine essentially makes two checks, one for GP and one for
*** Disc.  Java must also make these.  Use the GP.ERR to determine which
*** of these to check for (otherwords one can overwrite the other).  If
*** GP.ERR is not null, then Java has already make the GP Check and can
*** continue on to the Discount check.
          IF JAVA.SPEC AND NOT(GP.CHECKED) THEN
             GP.ERR = TEMP.ERR
          END ELSE IF (GP.CHECKED) THEN
             GP.ERR = ''
             TEMP.ERR = ''
          END
          IF NOT(JAVA.SPEC) THEN
             IF MAX.DS#'' AND NOT(PRC.ERROR) AND NOT(GOT.AUTH) THEN
               GOSUB CHK.DISC
             END
          END ELSE
             IF NOT(GP.CHANGED) AND MAX.DS#''THEN
                GOSUB CHK.DISC
             END
          END

*** Only write the gp information to the record if there was a change
          IF JAVA.SPEC AND GP.CHANGED THEN
             WRITE GP.INFO ON CTRLFILE,"GP.ERRORS~":USER.ID:"~":OID
          END

          GP.ERR = TEMP.ERR
          RETURN
*-------------------------------------------------------------------------*
GET.GP:   **
          BEGIN CASE
          CASE NO.LD
             GP.MODE = ""
             OLD.VALUE = ""
             IF IQPRC + 0 = 0 THEN
                GP = 0
             END ELSE
                GP = ICONV(((IQPRC-IQCOGS)/IQPRC)*100,'MR1')
             END
          CASE IQCOGS # ''
             IF NOT(COMM.MODE) THEN
                GP.MODE = "COGS"
                OLD.VALUE = LD(10)<1,GEN>
                OVERRIDEN = (LD(11)<1,GEN> # "")
             END ELSE
                GP.MODE = "COMM"
                OLD.VALUE = LD(27)<1,GEN>
                OVERRIDEN = (LD(28)<1,GEN> # "")
             END
             IF (LD(8)<1,GEN>+0) = 0 THEN GP = 0 ELSE
                GP = ICONV(((LD(8)<1,GEN>-IQCOGS)/LD(8)<1,GEN>)*100,'MR1')
             END
          CASE OTHERWISE
*** If GP% check is being initiated due to a price change we need to check
*** control record to determine if we should base check off of COGS
*** (default) or COMM-COST
             CTRB.ID = 'OE.MIN.GP.COST~':LED(2)<1,GEN,1>
             READ GP.COST FROM CTRBFILE,CTRB.ID ELSE GP.COST = NO
             IF GP.COST THEN COST = LD(27)<1,GEN> ELSE COST = LD(10)<1,GEN>

             GP.MODE   = "PRICE"
             OLD.VALUE = LD(8)<1,GEN>
             OVERRIDEN = (LD(9)<1,GEN> # "")
             * If IQPRC is not passed in, retrieve it from Ledger Detail
             IF IQPRC  = '' THEN IQPRC = LD(8)<1,GEN>
             IF IQPRC+0=0 THEN GP = 0 ELSE
                GP = ICONV(((IQPRC-COST) / IQPRC)*100,'MR1')
             END
          END CASE

          *********if direct GP% is passed in, set gp to it
          IF NOT(NO.LD) AND GP.DIRECT # '' THEN
             GP = GP.DIRECT
          END

          *** See if there was a Min. GP% override...
          MIN.GP = LED(35)<1,MIN.GP.VM>
          ITEM = "the Order";                       ** The order level.

          IF MIN.GP = '' THEN
             *** Get the pricing branch...
             BR = LED(2)<1,GEN,1>

             *** Set the minimum GP% based on the most detailed level at
             *** which it is set...
             IF NOT(NO.LD) THEN
                PRD.BR.GET.VAL BR,LD(1),50,PRD.MIN
                IF PRD.MIN<1,MIN.GP.SVM> THEN
                   *** Product level
                   MIN.GP = PRD.MIN<1,MIN.GP.SVM>
                   ITEM = 'the Product'
                END ELSE
                   PLNE.BR.GET.VAL BR,PRD(9),5,PLN.MIN
                   IF PLN.MIN<1,MIN.GP.SVM> THEN
                      *** Priceline level
                      MIN.GP = PLN.MIN<1,MIN.GP.SVM>
                      ITEM = 'the Price Line'
                   END ELSE
                      MIN.GP = SECURITY<24,MIN.GP.VM>
                      ITEM = 'the User Defined'
                   END
                END
             END ELSE
                MIN.GP = SECURITY<24,MIN.GP.VM>
                ITEM = 'the User Defined'
             END
          END

          RETURN
*-------------------------------------------------------------------------*
CHK.GP:
***  Check QS for closed Gen ELSE check open qty
          IF NOT(NO.LD) THEN
             IF LED(8)<1,GEN> # '' THEN
                QS = SUMMATION(LD(5)) + SUMMATION(LD(6))
             END ELSE
                OE.GET.QSIGN QSIGN,OID,GEN
                OE.CALC.QOPEN OID,QSIGN,QS
             END
          END ELSE
             QS = 1
          END

*** Quantity Sold for SubTotal = 0 so do the calculation for subtotals

PL = PRD(9)
IF PL='FP' OR PL='F8P' OR PL='FPC' OR PL='FPD' OR PL='FFG'
THEN MIN.GP='0'

IF USER.ID='TSMITH' OR USER.ID='BOBN' OR USER.ID='TOMS' OR USER.ID='MARKW' OR USER.ID='LORIM'
THEN MIN.GP=''


          IF (GP < MIN.GP) AND ((QS # 0) OR LD(1)='S' OR (JAVA.SPEC AND NOT(LD(1)))) THEN
             M.GP = OCONV(MIN.GP,'MR1')

             PRMPT= 'Allow Pricing Below ':ITEM:' Minimum GP% of ':M.GP:'%'
             IF NOT(NO.LD) THEN
                LD.GETV PN,LDID,1
                READV DESC FROM PRDFILE,PN,1 ELSE DESC = ''
                LOG.CMT = 'Pricing Below ':ITEM:' Minimum GP% - ':DESC<1,1>
             END ELSE
                IF LOG.CMT # '' THEN
                   LOG.CMT = 'Pricing Below ':ITEM:' Minimum GP%':LOG.CMT
                END
             END
             GP.ID = 'OE.GP.CHECK~GP~':OID:'~':LDID:'~':GEN
             GP.KEY= 'SOE.MIN.GP'
             LOCATE GP.ID IN RESPS<1> SETTING RPOS THEN
                IF NOT(RESPS<2,RPOS,1>) THEN PRC.ERROR = YES
             END ELSE
                BEGIN CASE
                CASE DO.ACTS
                   STOP.FLOW = YES
                   ACTION.ADD.AUTH ACTIONS,GP.ID,,PRMPT,,GP.KEY,,LOG.CMT,OID,GEN
                CASE NOT(JAVA.SPEC)
                   OE.GET.AUTH OID,PRMPT,GP.KEY,ACTION.OK,GEN,LOG.CMT
                   IF NOT(ACTION.OK) THEN PRC.ERROR=YES ELSE GOT.AUTH = YES
                CASE OTHERWISE
                   GP.CHANGED  = YES

                   IF NOT(NO.LD) THEN
                      IF (LDID) THEN
                         GP.INFO.ID = LDID:VM:GP.KEY:VM:GP.MODE:VM:OLD.VALUE:VM:PRMPT:VM:LOG.CMT:VM:OVERRIDEN
                         ** Don't add this if it's already present
                         LOCATE GP.INFO.ID IN GP.INFO SETTING POS ELSE
                            GP.INFO<-1> = GP.INFO.ID
                         END
                      END
                   END

                   TEMP.ERR = "SOE.MIN.GP":VM:GP.MODE:VM:OLD.VALUE:VM:PRMPT:VM:LOG.CMT:VM
                END CASE
             END
          END

          RETURN
*-------------------------------------------------------------------------*
CHK.DISC: * Check Discount
          IF NOT(NO.LD) THEN
             ORIG.PRC = LD(9)<1,GEN>
             IF ORIG.PRC='' THEN ORIG.PRC = LD(8)<1,GEN>
          END ELSE
             ORIG.PRC = OLD.IQPRC
          END

          IF ORIG.PRC+0=0 THEN
             CHNG = 0
          END ELSE
             CHNG = ICONV((ORIG.PRC-IQPRC) / ORIG.PRC * 100,'MR1')
          END

          IF (CHNG > MAX.DS) AND IQPRC THEN
             PRMPT = 'Exceed Maximum Discount'
             IF NOT(NO.LD) THEN
                LD.GETV PN,LDID,1
                READV DESC FROM PRDFILE,PN,1 ELSE DESC = ''
                LOG.CMT = 'Exceed Maximum Discount - ':DESC<1,1>
             END ELSE
                IF LOG.CMT # '' THEN
                   LOG.CMT = 'Exceed Maximum Discount - ':LOG.CMT
                END
             END
             DISC.ID = 'OE.GP.CHECK~DISC~':OID:'~':LDID:'~':GEN
             DISC.KEY = 'SOE.MAX.DISC'
             LOCATE DISC.ID IN RESPS<1> SETTING RPOS THEN
                IF NOT(RESPS<2,RPOS,1>) THEN PRC.ERROR = YES
             END ELSE
                BEGIN CASE
                CASE DO.ACTS
                   STOP.FLOW = YES
                   ACTION.ADD.AUTH ACTIONS,DISC.ID,,PRMPT,,DISC.KEY,,LOG.CMT,OID,GEN
                CASE NOT(JAVA.SPEC)
                   OE.GET.AUTH OID,PRMPT,'SOE.MAX.DISC',ACTION.OK,GEN,LOG.CMT
                   IF NOT(ACTION.OK) THEN PRC.ERROR=YES
                CASE OTHERWISE
                   GP.CHANGED  = YES
                   IF (LDID) THEN
                      GP.INFO.ID = LDID:VM:'SOE.MAX.DISC':VM:GP.MODE:VM:OLD.VALUE:VM:PRMPT:VM:LOG.CMT:VM:OVERRIDEN
                      ** Don't add this if it's already present
                      LOCATE GP.INFO.ID IN GP.INFO SETTING POS ELSE
                         GP.INFO<-1> = GP.INFO.ID
                      END
                   END
                   TEMP.ERR = 'SOE.MAX.DISC':VM:GP.MODE:VM:OLD.VALUE:VM:PRMPT:VM:LOG.CMT:VM
                END CASE
             END
          END

          RETURN
!TSMITH~04/10/14~16:31
